package cn.modoumama.page;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;

@SuppressWarnings("unchecked")
public class Page implements Serializable {
	private static final long serialVersionUID = -7932707520345647098L;
	private int totalCount = 0;

	private int pageSize = 10;

	private int currPage = 1;

	private Map<String, Object> condition = new HashMap<String, Object>();

	private boolean exeQuery = true;

	public String toString() {
		return "PageObject(totalCount=" + getTotalCount() + ", pageSize=" + getPageSize() + ", currPage="
				+ getCurrPage() + ", exeQuery=" + isExeQuery() + ")";
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Page))
			return false;
		Page other = (Page) obj;
		if (getTotalCount() != other.getTotalCount())
			return false;
		if (getPageSize() != other.getPageSize())
			return false;
		if (getCurrPage() != other.getCurrPage())
			return false;
		if (getCondition().size() != other.getCondition().size())
			return false;
		if (isExeQuery() != other.isExeQuery())
			return false;
		return true;
	}

	public int hashCode() {
		int PRIME = 31;
		int result = 1;
		result = result * PRIME + getTotalCount();
		result = result * PRIME + getPageSize();
		result = result * PRIME + getCurrPage();
		result = result * PRIME + (isExeQuery() ? 1231 : 1237);
		return result;
	}

	public int getTotalCount() {
		return this.totalCount;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public boolean isExeQuery() {
		return this.exeQuery;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public void setExeQuery(boolean exeQuery) {
		this.exeQuery = exeQuery;
	}

	public int getCurrPage() {
		return this.currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public int getOffset() {
		return this.pageSize * (this.currPage - 1);
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public Map<String, Object> getCondition() {
		return this.condition;
	}

	public void setConditions(Object obj) {
		try {
			if (obj != null) {
				condition = PropertyUtils.describe(obj);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void addConditions(Object obj) {
		try {
			condition.putAll(PropertyUtils.describe(obj));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void addCondition(String key, Object value) {
		if (value != null)
			condition.put(key, value);
	}

	public void removeCondition(String key, Object value) {
		if (value != null)
			condition.remove(key);
	}

	public void clearConditions() {
		condition.clear();

	}
}
